using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using System.Data;
using Slaks.Graphs.GraphBuilder;

namespace SchenkerGraphsCommonality
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button m_parsedFileListBtn;
		private System.Windows.Forms.Button m_outputDirBtn;
		private System.Windows.Forms.Button m_startBtn;
		private System.Windows.Forms.TextBox m_parsedFileListTB;
		private System.Windows.Forms.GroupBox m_parsedFileListGB;
		private System.Windows.Forms.GroupBox m_outputDirGB;
		private System.Windows.Forms.TextBox m_outputDirTB;
		private System.Windows.Forms.OpenFileDialog openFileDialog;
		private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog;
		private System.Windows.Forms.NumericUpDown m_graphSizeUD;
		private System.Windows.Forms.Label m_graphSizeL;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.ProgressBar m_interFileProgress;
		private System.Windows.Forms.ProgressBar m_intraFileProgress;
		private System.Windows.Forms.GroupBox m_fileGB;
		private System.Windows.Forms.CheckBox m_onlyOneFileCB;
		private System.Windows.Forms.TextBox m_fileTB;
		private System.Windows.Forms.Button m_fileBtn;
		private System.Windows.Forms.OpenFileDialog openFileDialog2;
		private bool m_isOnlyOneFile;
		private System.Windows.Forms.GroupBox m_representationGB;
		private System.Windows.Forms.ComboBox m_representationCB;

		private int m_graphSize;
		private System.Windows.Forms.CheckBox m_holdParsingCB;
		private BuilderType m_graphBuilderType;
		private bool m_isHoldPreviousSeriesParsed;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			m_graphSize = (int) m_graphSizeUD.Value;
			m_isOnlyOneFile = m_fileGB.Enabled = m_onlyOneFileCB.Checked;

			m_representationCB.Items.Add(BuilderType.StandardRepresentationSlaks);
			m_representationCB.Items.Add(BuilderType.RawFrequencyRepresentationSlaks);
			m_representationCB.Items.Add(BuilderType.NormalizedFrequencyRepresentationSlaks);
			m_representationCB.SelectedIndex = 0;
			m_graphBuilderType = BuilderType.StandardRepresentationSlaks;
			m_isHoldPreviousSeriesParsed = m_holdParsingCB.Checked;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.m_parsedFileListTB = new System.Windows.Forms.TextBox();
			this.m_parsedFileListBtn = new System.Windows.Forms.Button();
			this.m_parsedFileListGB = new System.Windows.Forms.GroupBox();
			this.m_outputDirGB = new System.Windows.Forms.GroupBox();
			this.m_outputDirBtn = new System.Windows.Forms.Button();
			this.m_outputDirTB = new System.Windows.Forms.TextBox();
			this.m_interFileProgress = new System.Windows.Forms.ProgressBar();
			this.m_startBtn = new System.Windows.Forms.Button();
			this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
			this.folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
			this.m_graphSizeUD = new System.Windows.Forms.NumericUpDown();
			this.m_graphSizeL = new System.Windows.Forms.Label();
			this.m_intraFileProgress = new System.Windows.Forms.ProgressBar();
			this.m_fileGB = new System.Windows.Forms.GroupBox();
			this.m_fileBtn = new System.Windows.Forms.Button();
			this.m_fileTB = new System.Windows.Forms.TextBox();
			this.m_onlyOneFileCB = new System.Windows.Forms.CheckBox();
			this.openFileDialog2 = new System.Windows.Forms.OpenFileDialog();
			this.m_representationGB = new System.Windows.Forms.GroupBox();
			this.m_representationCB = new System.Windows.Forms.ComboBox();
			this.m_holdParsingCB = new System.Windows.Forms.CheckBox();
			this.m_parsedFileListGB.SuspendLayout();
			this.m_outputDirGB.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.m_graphSizeUD)).BeginInit();
			this.m_fileGB.SuspendLayout();
			this.m_representationGB.SuspendLayout();
			this.SuspendLayout();
			// 
			// m_parsedFileListTB
			// 
			this.m_parsedFileListTB.Location = new System.Drawing.Point(16, 32);
			this.m_parsedFileListTB.Name = "m_parsedFileListTB";
			this.m_parsedFileListTB.Size = new System.Drawing.Size(560, 20);
			this.m_parsedFileListTB.TabIndex = 0;
			this.m_parsedFileListTB.Text = "";
			// 
			// m_parsedFileListBtn
			// 
			this.m_parsedFileListBtn.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.m_parsedFileListBtn.Location = new System.Drawing.Point(592, 32);
			this.m_parsedFileListBtn.Name = "m_parsedFileListBtn";
			this.m_parsedFileListBtn.TabIndex = 1;
			this.m_parsedFileListBtn.Text = "...";
			this.m_parsedFileListBtn.Click += new System.EventHandler(this.ButtonClickHandler);
			// 
			// m_parsedFileListGB
			// 
			this.m_parsedFileListGB.Controls.Add(this.m_parsedFileListBtn);
			this.m_parsedFileListGB.Controls.Add(this.m_parsedFileListTB);
			this.m_parsedFileListGB.Location = new System.Drawing.Point(16, 16);
			this.m_parsedFileListGB.Name = "m_parsedFileListGB";
			this.m_parsedFileListGB.Size = new System.Drawing.Size(720, 80);
			this.m_parsedFileListGB.TabIndex = 4;
			this.m_parsedFileListGB.TabStop = false;
			this.m_parsedFileListGB.Text = "Parsed file list";
			// 
			// m_outputDirGB
			// 
			this.m_outputDirGB.Controls.Add(this.m_outputDirBtn);
			this.m_outputDirGB.Controls.Add(this.m_outputDirTB);
			this.m_outputDirGB.Location = new System.Drawing.Point(16, 112);
			this.m_outputDirGB.Name = "m_outputDirGB";
			this.m_outputDirGB.Size = new System.Drawing.Size(720, 80);
			this.m_outputDirGB.TabIndex = 5;
			this.m_outputDirGB.TabStop = false;
			this.m_outputDirGB.Text = "Output directory";
			// 
			// m_outputDirBtn
			// 
			this.m_outputDirBtn.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.m_outputDirBtn.Location = new System.Drawing.Point(592, 32);
			this.m_outputDirBtn.Name = "m_outputDirBtn";
			this.m_outputDirBtn.TabIndex = 1;
			this.m_outputDirBtn.Text = "...";
			this.m_outputDirBtn.Click += new System.EventHandler(this.ButtonClickHandler);
			// 
			// m_outputDirTB
			// 
			this.m_outputDirTB.Location = new System.Drawing.Point(16, 32);
			this.m_outputDirTB.Name = "m_outputDirTB";
			this.m_outputDirTB.Size = new System.Drawing.Size(560, 20);
			this.m_outputDirTB.TabIndex = 0;
			this.m_outputDirTB.Text = "";
			// 
			// m_interFileProgress
			// 
			this.m_interFileProgress.Location = new System.Drawing.Point(16, 416);
			this.m_interFileProgress.Name = "m_interFileProgress";
			this.m_interFileProgress.Size = new System.Drawing.Size(720, 16);
			this.m_interFileProgress.Step = 1;
			this.m_interFileProgress.TabIndex = 6;
			// 
			// m_startBtn
			// 
			this.m_startBtn.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.m_startBtn.Location = new System.Drawing.Point(616, 488);
			this.m_startBtn.Name = "m_startBtn";
			this.m_startBtn.TabIndex = 7;
			this.m_startBtn.Text = "Start";
			this.m_startBtn.Click += new System.EventHandler(this.ButtonClickHandler);
			// 
			// openFileDialog
			// 
			this.openFileDialog.Filter = "Parsed file lis (xml)|*.xml|All files|*.*";
			this.openFileDialog.RestoreDirectory = true;
			this.openFileDialog.Title = "Choose parsed file list (xml)";
			// 
			// folderBrowserDialog
			// 
			this.folderBrowserDialog.Description = "Choose output directory";
			this.folderBrowserDialog.RootFolder = System.Environment.SpecialFolder.MyComputer;
			// 
			// m_graphSizeUD
			// 
			this.m_graphSizeUD.Location = new System.Drawing.Point(664, 208);
			this.m_graphSizeUD.Maximum = new System.Decimal(new int[] {
																																	1000,
																																	0,
																																	0,
																																	0});
			this.m_graphSizeUD.Name = "m_graphSizeUD";
			this.m_graphSizeUD.Size = new System.Drawing.Size(72, 20);
			this.m_graphSizeUD.TabIndex = 8;
			this.m_graphSizeUD.Value = new System.Decimal(new int[] {
																																50,
																																0,
																																0,
																																0});
			this.m_graphSizeUD.ValueChanged += new System.EventHandler(this.GraphSizeChangeHandler);
			// 
			// m_graphSizeL
			// 
			this.m_graphSizeL.Location = new System.Drawing.Point(592, 210);
			this.m_graphSizeL.Name = "m_graphSizeL";
			this.m_graphSizeL.Size = new System.Drawing.Size(64, 16);
			this.m_graphSizeL.TabIndex = 9;
			this.m_graphSizeL.Text = "Graph Size";
			// 
			// m_intraFileProgress
			// 
			this.m_intraFileProgress.Location = new System.Drawing.Point(16, 448);
			this.m_intraFileProgress.Name = "m_intraFileProgress";
			this.m_intraFileProgress.Size = new System.Drawing.Size(720, 16);
			this.m_intraFileProgress.Step = 1;
			this.m_intraFileProgress.TabIndex = 10;
			// 
			// m_fileGB
			// 
			this.m_fileGB.Controls.Add(this.m_fileBtn);
			this.m_fileGB.Controls.Add(this.m_fileTB);
			this.m_fileGB.Location = new System.Drawing.Point(16, 200);
			this.m_fileGB.Name = "m_fileGB";
			this.m_fileGB.Size = new System.Drawing.Size(560, 80);
			this.m_fileGB.TabIndex = 11;
			this.m_fileGB.TabStop = false;
			this.m_fileGB.Text = "File";
			// 
			// m_fileBtn
			// 
			this.m_fileBtn.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.m_fileBtn.Location = new System.Drawing.Point(504, 32);
			this.m_fileBtn.Name = "m_fileBtn";
			this.m_fileBtn.Size = new System.Drawing.Size(40, 24);
			this.m_fileBtn.TabIndex = 1;
			this.m_fileBtn.Text = "...";
			this.m_fileBtn.Click += new System.EventHandler(this.ButtonClickHandler);
			// 
			// m_fileTB
			// 
			this.m_fileTB.Location = new System.Drawing.Point(16, 32);
			this.m_fileTB.Name = "m_fileTB";
			this.m_fileTB.Size = new System.Drawing.Size(472, 20);
			this.m_fileTB.TabIndex = 0;
			this.m_fileTB.Text = "";
			// 
			// m_onlyOneFileCB
			// 
			this.m_onlyOneFileCB.Location = new System.Drawing.Point(592, 256);
			this.m_onlyOneFileCB.Name = "m_onlyOneFileCB";
			this.m_onlyOneFileCB.Size = new System.Drawing.Size(144, 24);
			this.m_onlyOneFileCB.TabIndex = 12;
			this.m_onlyOneFileCB.Text = "Only one file";
			this.m_onlyOneFileCB.Click += new System.EventHandler(this.ButtonClickHandler);
			// 
			// openFileDialog2
			// 
			this.openFileDialog2.Filter = "Parsed file (Xml)|*.xml|All files|*.*";
			this.openFileDialog2.RestoreDirectory = true;
			this.openFileDialog2.Title = "Choose parsed file to check";
			// 
			// m_representationGB
			// 
			this.m_representationGB.Controls.Add(this.m_representationCB);
			this.m_representationGB.Location = new System.Drawing.Point(16, 296);
			this.m_representationGB.Name = "m_representationGB";
			this.m_representationGB.Size = new System.Drawing.Size(728, 64);
			this.m_representationGB.TabIndex = 13;
			this.m_representationGB.TabStop = false;
			this.m_representationGB.Text = "Representation";
			// 
			// m_representationCB
			// 
			this.m_representationCB.Location = new System.Drawing.Point(112, 24);
			this.m_representationCB.Name = "m_representationCB";
			this.m_representationCB.Size = new System.Drawing.Size(512, 21);
			this.m_representationCB.TabIndex = 0;
			this.m_representationCB.SelectionChangeCommitted += new System.EventHandler(this.ButtonClickHandler);
			// 
			// m_holdParsingCB
			// 
			this.m_holdParsingCB.Checked = true;
			this.m_holdParsingCB.CheckState = System.Windows.Forms.CheckState.Checked;
			this.m_holdParsingCB.Location = new System.Drawing.Point(16, 376);
			this.m_holdParsingCB.Name = "m_holdParsingCB";
			this.m_holdParsingCB.Size = new System.Drawing.Size(184, 24);
			this.m_holdParsingCB.TabIndex = 14;
			this.m_holdParsingCB.Text = "Hold previous series parsing?";
			this.m_holdParsingCB.Click += new System.EventHandler(this.ButtonClickHandler);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(752, 526);
			this.Controls.Add(this.m_holdParsingCB);
			this.Controls.Add(this.m_representationGB);
			this.Controls.Add(this.m_onlyOneFileCB);
			this.Controls.Add(this.m_fileGB);
			this.Controls.Add(this.m_intraFileProgress);
			this.Controls.Add(this.m_graphSizeL);
			this.Controls.Add(this.m_graphSizeUD);
			this.Controls.Add(this.m_startBtn);
			this.Controls.Add(this.m_interFileProgress);
			this.Controls.Add(this.m_outputDirGB);
			this.Controls.Add(this.m_parsedFileListGB);
			this.Name = "Form1";
			this.Text = "Schenker Graphs Commonality (Wordnet as stemming tool)";
			this.m_parsedFileListGB.ResumeLayout(false);
			this.m_outputDirGB.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.m_graphSizeUD)).EndInit();
			this.m_fileGB.ResumeLayout(false);
			this.m_representationGB.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.EnableVisualStyles();
			Application.Run(new Form1());
		}

		private void ButtonClickHandler(object sender, System.EventArgs e)
		{
			try
			{
				if (sender == m_parsedFileListBtn) this.ChooseParsedFileList();
				else if (sender == m_outputDirBtn) this.ChooseOutputDirectory();
				else if (sender == m_startBtn) this.Start();
				else if (sender == m_fileBtn) this.ChooseFile();
				else if (sender == m_onlyOneFileCB) this.SetOnlyOneFile(m_onlyOneFileCB.Checked);
				else if (sender == m_representationCB) this.ChangeRepresentation();
				else if (sender == m_holdParsingCB) this.SetPreviousDocumentParsedFlag();
			}catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void SetPreviousDocumentParsedFlag()
		{
			m_isHoldPreviousSeriesParsed = m_holdParsingCB.Checked;
		}

		private void ChangeRepresentation()
		{
			m_graphBuilderType = (BuilderType)BuilderType.Parse(typeof(BuilderType),m_representationCB.SelectedItem.ToString());
		}
		private void ChooseFile()
		{
			if (openFileDialog2.ShowDialog() == DialogResult.OK)
			{
				m_fileTB.Text = openFileDialog2.FileName;
			}
		}

		private void SetOnlyOneFile(bool onlyOne)
		{
				m_fileGB.Enabled = m_isOnlyOneFile = onlyOne;
		}

		private void ChooseParsedFileList()
		{
			if (openFileDialog.ShowDialog() == DialogResult.OK)
			{
				m_parsedFileListTB.Text = openFileDialog.FileName;
			}
		}

		private void ChooseOutputDirectory()
		{
			if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
			{
				m_outputDirTB.Text = folderBrowserDialog.SelectedPath;
				if (m_outputDirTB.Text[m_outputDirTB.Text.Length - 1] != '\\')
					m_outputDirTB.Text += "\\";
			}
		}

		private void Start()
		{
			if (m_outputDirTB.Text[m_outputDirTB.Text.Length - 1] != '\\') m_outputDirTB.Text += "\\";
			if (File.Exists(m_parsedFileListTB.Text) == false) throw new Exception("Chosen parsed file list doesn't exist");
			if (File.Exists(m_fileTB.Text) == false) throw new Exception("Chosen parsed file doesn't exist");
			if (Directory.Exists(m_outputDirTB.Text) == false ) Directory.CreateDirectory(m_outputDirTB.Text);
			ProcessMediator mediator = new ProcessMediator(m_interFileProgress,
																											m_intraFileProgress,
																												m_parsedFileListTB.Text,
																													m_outputDirTB.Text,
																														(int)m_graphSizeUD.Value,
																															m_isOnlyOneFile,
																																m_fileTB.Text,
																																	m_graphBuilderType,
																																		m_isHoldPreviousSeriesParsed);
			mediator.Start();
		}

		private void GraphSizeChangeHandler(object sender, System.EventArgs e)
		{
		  m_graphSize = (int) m_graphSizeUD.Value;
		}
	}
}
